package com.iscas.tech.controller;

import com.iscas.tech.entity.AtNewsattach;
import com.iscas.tech.param.AtNewsattachPageParam;
import com.iscas.tech.service.AtNewsattachService;
import com.iscas.tech.vo.AtNewsattachQueryVo;
import io.geekidea.springbootplus.framework.common.api.ApiCode;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.pagination.Paging;
import io.geekidea.springbootplus.framework.shiro.util.LoginUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 新闻附件表 控制器
 *
 * @author yangyi
 * @since 2020-10-26
 */
@Slf4j
@RestController
@RequestMapping("/newsFile")
@Api(value = "新闻附件表API", tags = {"新闻附件表"})
public class AtNewsattachController extends BaseController {

    @Autowired
    private AtNewsattachService atNewsattachService;

    /**
     * 添加新闻附件表
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加新闻附件表", response = ApiResult.class)
    public ApiResult<Boolean> addAtNewsattach(AtNewsattach atNewsattach) throws Exception {
        if (LoginUtil.getUserId() == null) {
            return ApiResult.fail(ApiCode.UNAUTHORIZED);
        }
        atNewsattach.setCreatetitle(LoginUtil.getUserId().intValue());
        boolean flag = atNewsattachService.saveAtNewsattach(atNewsattach);
        return ApiResult.result(flag);
    }

    /**
     * 修改新闻附件表
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改新闻附件表", response = ApiResult.class)
    public ApiResult<Boolean> updateAtNewsattach(AtNewsattach atNewsattach) throws Exception {
        boolean flag = atNewsattachService.updateAtNewsattach(atNewsattach);
        return ApiResult.result(flag);
    }

    /**
     * 删除新闻附件表
     */
    @RequestMapping(value = "delete" ,produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除新闻附件表", response = ApiResult.class)
    public ApiResult<Boolean> deleteAtNewsattach(String ids) throws Exception {
        String idArr[] = ids.split(",");
        for (String id : idArr) {
            atNewsattachService.deleteAtNewsattach(Long.parseLong(id));
        }

        return ApiResult.ok();
    }

    /**
     * 获取新闻附件表详情
     */
    @RequestMapping(value = "info" ,produces = "application/json; charset=utf-8")
    @ApiOperation(value = "新闻附件表详情", response = AtNewsattach.class)
    public ApiResult<AtNewsattach> getAtNewsattach(Long id) throws Exception {
        AtNewsattach atNewsattach = atNewsattachService.getById(id);
        return ApiResult.ok(atNewsattach);
    }

    /**
     * 新闻附件表分页列表
     */
    @PostMapping("/getPageList")
    @ApiOperation(value = "新闻附件表分页列表", response = AtNewsattach.class)
    public ApiResult<Paging<AtNewsattachQueryVo>> getAtNewsattachPageList(AtNewsattachPageParam atNewsattachPageParam) throws Exception {
        Paging<AtNewsattachQueryVo> paging = atNewsattachService.getAtNewsattachPageList(atNewsattachPageParam);
        return ApiResult.ok(paging);
    }

}

